<?php

namespace App\Http\Controllers;

use App\Models\Customer;
use App\Models\CustomerRole;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Whoops\Exception\ErrorException;

class CustomerController extends Controller
{
    /**
     * 用户详情
     * GET /api/customer/detail
     * Display a listing of the resource.
     * @throws ErrorException
     */
    public function detail(Request $request): \Illuminate\Http\JsonResponse
    {
        $params = $request->all();

        $data = (new Customer())->getDetail($params);

        $data = current($data);
        $data->role = CustomerRole::where('customer_id', $params['customer_id'])->get();

        return response()->json(['status' => 1, 'data' => $data]);
    }

    /**
     * 用户信息编辑
     * PUT /api/customer/update
     * @param Request $request
     * @return JsonResponse
     */
    public function update(Request $request): \Illuminate\Http\JsonResponse
    {
        $params = $request->all();

        $data['nickname'] = $params['nickname'];
        $data['six'] = $params['six'];
        $data['age'] = $params['age'];
        $data['weight'] = $params['weight'];
        $data['phone'] = $params['phone'];
        $data['birthday'] = $params['birthday'];
        Customer::where('id', $params['customer_id'])->update($data);

        $data = (new Customer())->getDetail($params);

        $data = current($data);
        $data->role = CustomerRole::where('customer_id', $params['customer_id'])->get();

        return response()->json(['status' => 1, 'data' => $data]);
    }
}
